package org.nowcoder.leetcode.slidingWindow;

/**
 * Title  : 209. Minimum Size Subarray Sum
 * Source : https://leetcode.cn/problems/minimum-size-subarray-sum/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-21
 */

public class LeetCode_209 {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0, right = 0; //[left,right]
        int sum = 0, len = nums.length + 1;
        while (right < nums.length) {
            sum += nums[right];
            if (sum >= target) {
                while (sum >= target) {
                    len = Math.min(len, right - left + 1);
                    sum -= nums[left++];
                }
            }
            right++;
        }
        return len == nums.length + 1 ? 0 : len;
    }
}
